package com.niuniu;

import com.xingli.zhajinhua.beans.Card;
import com.xingli.zhajinhua.beans.PlayerCards;

import java.util.ArrayList;
import java.util.List;

public abstract class AbstractCardsGame implements CardsGameLogic {


    //玩家人数
    public int playerNumber;
    //一个玩家有多少张牌
    public int playerCardNumber;
    //牌的权值倍数
    public int baseWeight;


    public AbstractCardsGame(int playerNumber, int playerCardNumber, int baseWeight) {
        this.playerCardNumber = playerCardNumber;
        this.playerNumber = playerNumber;
        this.baseWeight = baseWeight;
    }


    @Override
    public int getPlayerNumber() {
        return playerNumber;
    }

    @Override
    public int getPlayerCardsNumber() {
        return playerCardNumber;
    }
    //比较牌
    @Override
    public List<PlayerCards> compareCards(List<Card[]> cardList) {
        List<PlayerCards> playerCards = new ArrayList<>(playerNumber);
        int index = 0;
        for (Card[] cards : cardList) {
            bubbleSort(cards);
            PlayerCards cardsData = new PlayerCards();
            cardsData.setCardsType(judgeCardType(cards));
            cardsData.setIndex(index);
            cardsData.setCards(cards);
            cardsData.setWeight(calculateCardsWeight(cards, cardsData.getCardsType()));
            cardsData.setResult(false);
            playerCards.add(cardsData);
            index++;
        }
        int weight = 0;
        PlayerCards winPlayer = null;
        for (PlayerCards cardsData : playerCards) {
            if (cardsData.getWeight() > weight) {
                winPlayer = cardsData;
                weight = winPlayer.getWeight();
            }
        }
        assert winPlayer != null;
        winPlayer.setResult(true);
        return playerCards;
    }



    //发牌
    @Override
    public List<Card[]> sendCards(List<Card> cardList) {
        List<Card[]> cards = new ArrayList<>(playerNumber);
        int index = 0;
        for (int i = 0; i < playerNumber; i++) {
            Card[] playerCard = new Card[playerCardNumber];
            for (int j = 0; j < playerCardNumber; j++) {
                playerCard[j] = cardList.get(index);
                index++;
            }
            cards.add(playerCard);
        }
        return cards;
    }

    //冒泡排序
    public void bubbleSort(Card[] cards) {
        for (int i = 0; i < cards.length; i++) {
            for (int j = 0; j < cards.length - 1; j++) {
                if (cards[j + 1].getWeight() < cards[j].getWeight()) {
                    Card temp = cards[j];
                    cards[j] = cards[j + 1];
                    cards[j + 1] = temp;
                }
            }
        }
    }
}
